package per.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author joker
 * @date 2022/5/13 0013 - 2:15
 */
public class ProxyFactory {
    public static Object getProxyInstance(Object obj){
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(),
                obj.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("开始计算"+args[0]+","+args[1]);
                        Object returnValue = method.invoke(obj, args);
                        System.out.println("结果是："+returnValue);
                        System.out.println("结束计算");
                        return returnValue;
                    }
                });
    }

    public static void main(String[] args) {
//        Cal cal=new Caculater();
//        Caculater cal1=new Caculater();
//        Cal2 caculater = (Cal2) getProxyInstance(cal);
////        caculater.add(1, 2);
////        caculater.minus(1, 2);
//        caculater.mult(2,3);
    }
}
